package manager.cms.service.exec;

import common.SnowflakeIdWorker;
import manager.cms.dao.CmsContentCatDao;
import manager.cms.dao.CmsContentDao;
import manager.cms.service.JdExecutor;
import manager.cms.vo.CmsContentCatVO;
import manager.cms.vo.CmsContentVO;
import manager.feign.ShopIndexFloorThirdService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "marketBillExec")
public class MarketBillExecImpl implements JdExecutor {
    @Autowired
    private ShopIndexFloorThirdService shopIndexFloorThirdService;
    @Autowired
    private CmsContentCatDao cmsContentCatDao;
    @Autowired
    private CmsContentDao cmsContenDao;
    @Override
    public void execute(Map<String,Object> mapParams) {
        try {
            CmsContentCatVO cmsContentCatVO=new CmsContentCatVO();
            JSONObject jsonObject = null;
            List<CmsContentVO> contentVOList=new ArrayList<CmsContentVO>();
            String marketBill = shopIndexFloorThirdService.getMarketBill();
            String todayMonetizeStr=marketBill.substring(12,marketBill.length()-1);
            jsonObject = new JSONObject(todayMonetizeStr);
            JSONArray todayMonetizeData=(JSONArray)jsonObject.get("data");
            String focusMonetizeCmsContentId=String.valueOf(SnowflakeIdWorker.nextId());
            cmsContentCatVO.setName("频道广场");
            cmsContentCatVO.setId(focusMonetizeCmsContentId);
            cmsContentCatDao.addCmsContentCat(cmsContentCatVO);
            for(int i=0;i<todayMonetizeData.length();i++){
                CmsContentVO cmsContentVO=new CmsContentVO();
                String id=String.valueOf(SnowflakeIdWorker.nextId());
                JSONObject item=(JSONObject)todayMonetizeData.get(i);
                String urlList=item.get("bigLink").toString();
                String imageList=item.get("bigImg").toString();
                String nameList=item.get("name").toString();
                if(item.has("item")&&!item.get("item").equals("")){
                      JSONArray items=(JSONArray)item.get("item");
                      urlList=item.get("smallLink").toString();
                      imageList="";
                      nameList=item.get("name").toString()+";"+item.get("desc").toString();
                      for(int j=0;j<items.length();j++){
                          imageList=imageList+";"+"//img12.360buyimg.com/img/s200x200_"+((JSONObject)items.get(j)).get("img");
                      }
                }
                cmsContentVO.setId(id);
                cmsContentVO.setPic(imageList);
                cmsContentVO.setTitle(nameList+"->频道广场");
                cmsContentVO.setUrl(urlList);
                cmsContentVO.setCategoryId(focusMonetizeCmsContentId);
                contentVOList.add(cmsContentVO);

            }
            cmsContenDao.addCmsContentBatch(contentVOList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
